package org.ricks.ioc;

import org.ricks.ioc.utils.IocException;

import java.lang.reflect.Field;

/**
 * bean 解析处理器， 默认Bean
 */
public interface BeanAnalysisProcessor {

    default Object postProcessAfterInitialization(Object bean) throws IocException {
        return bean;
    }


    default void copyFieldValues(Object source, Object target) {
        Class<?> sourceClass = source.getClass();
        Class<?> targetClass = target.getClass();

        while (sourceClass != null && targetClass != null) {
            for (Field sourceField : sourceClass.getDeclaredFields()) {
                try {
                    Field targetField = targetClass.getDeclaredField(sourceField.getName());
                    if (targetField.getType().isAssignableFrom(sourceField.getType())) {
                        sourceField.setAccessible(true);
                        targetField.setAccessible(true);
                        targetField.set(target, sourceField.get(source));
                    }
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    // 忽略不存在的字段或访问异常
                }
            }
            sourceClass = sourceClass.getSuperclass();
            targetClass = targetClass.getSuperclass();
        }
    }
}
